package neusoft.example.com.yuejian06.Fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import neusoft.example.com.yuejian06.Bean.User;
import neusoft.example.com.yuejian06.Model.MyModel;
import neusoft.example.com.yuejian06.PublicIp;
import neusoft.example.com.yuejian06.R;
import neusoft.example.com.yuejian06.RetrofitListener;

import static android.content.Context.MODE_PRIVATE;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {
   private ImageView user_pic;
   private TextView user_name,user_tel;
   private Button clear;
   private LinearLayout update_information,update_password,my_picture,my_attention,my_order,my_question,my_reply,my_message;
   private String user_id;
   private SharedPreferences sp;
   private RetrofitListener<User> listener;
   private MyModel model;
   private Context context;

    public MyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_my, container, false);

        initView(view);

        sp=getActivity().getSharedPreferences("user_id",MODE_PRIVATE);
        user_id=sp.getString("user_id",null);

        listener();

        model=new MyModel();
        model.getMy(user_id,listener);

        initEvents();


        return view;
    }
    public  void initView(View view){
        user_pic=(ImageView) view.findViewById(R.id.user_pic);
        user_name=(TextView) view.findViewById(R.id.user_name);
        user_tel=(TextView) view.findViewById(R.id.user_tel);
        clear=(Button) view.findViewById(R.id.clear);
        update_information=(LinearLayout) view.findViewById(R.id.update_information);
        update_password=(LinearLayout) view.findViewById(R.id.update_password);
        my_picture=(LinearLayout) view.findViewById(R.id.my_picture);
        my_attention=(LinearLayout) view.findViewById(R.id.my_attention);
        my_order=(LinearLayout) view.findViewById(R.id.my_order);
        my_question=(LinearLayout) view.findViewById(R.id.my_question);
        my_reply=(LinearLayout) view.findViewById(R.id.my_reply);
        my_message=(LinearLayout) view.findViewById(R.id.my_message);

    }
    public  void listener(){
        listener=new RetrofitListener<User>() {
            @Override
            public void onResponse(User l) {
                user_name.setText(l.getUser_name());
                user_tel.setText(l.getUser_tel());
                Picasso.with(context)
                        .load(PublicIp.ip+"static/user_picture/"+l.getUser_pic())
                        .into(user_pic);
            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            }
        };
    }

    public void initEvents(){
        user_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        update_information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        update_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        my_picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        my_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        my_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        my_question.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        my_reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        my_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
}
